import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import { token } from '../apis/users'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/Index.vue'),
    children: [
      {
        path: '/index/main',
        name: 'main',
        component: () => import('../views/modules/Main.vue')
      },
      {
        path: '/index/order',
        name: 'order',
        component: () => import('../views/modules/OrderManager.vue')
      },
      {
        path: '/index/store',
        name: 'store',
        component: () => import('../views/modules/StoreManager.vue')
      },
      {
        path: '/index/accountadd',
        name: 'accountadd',
        component: () => import('../views/modules/account/AccountAdd.vue')
      },
      {
        path: '/index/accountlist',
        name: 'accountlist',
        component: () => import('../views/modules/account/AccountList.vue')
      },
      {
        path: '/index/setpwd',
        name: 'setpwd',
        component: () => import('../views/modules/account/SetPassword.vue')
      },
      {
        path: '/index/itemadd',
        name: 'itemadd',
        component: () => import('../views/modules/commodity/ItemAdd.vue')
      },
      {
        path: '/index/itemlist',
        name: 'itemlist',
        component: () => import('../views/modules/commodity/ItemList.vue')
      },
      {
        path: '/index/itemtype',
        name: 'itemtype',
        component: () => import('../views/modules/commodity/ItemType.vue')
      },
      {
        path: '/index/comtotal',
        name: 'comtotal',
        component: () => import('../views/modules/sales/CommodityTotal.vue')
      },
      {
        path: '/index/salestotal',
        name: 'salestotal',
        component: () => import('../views/modules/sales/SalesTotal.vue')
      },
      {
        path: '/index/mine',
        name: 'mine',
        component: () => import('../views/modules/account/Mine.vue')
      },
    ]
  },
  {
    path: '*',
    name: '404',
    component: () => import('../views/Errer404.vue')
  },
]

const router = new VueRouter({
  routes
})

//路由拦截
router.beforeEach((to, from, next) => {
  if (to.path == '/') {//如果用户去登录,放行
    next()
  } else {
    // console.log(from);
    token({ token: localStorage.token }).then(res => {
      if (res.data.code == 0) {
        next()
      } else {
        next('/')
      }
    })
  }
})
export default router
